package com.tools.common.web;


import javax.servlet.http.HttpServletResponse;

/**
 * @Author : ShangJY
 * @create 2024/1/15 10:38
 */
public class HttpUtil {

    /**
     * Http的请求头
     */
    public static class ReqHeaders{


        public static final String AUTHORIZATION = "Authorization";
    }

    /**
     * Http 的响应头
     */
    public static class ResHeaders{

        public static final String ACAO = "Access-Control-Allow-Origin";

        /**
         * 告诉客户端如何解析内容
         * text/html：HTML文档
         * application/json：JSON格式数据
         * image/jpeg：JPEG图片
         * application/pdf：PDF文档
         * text/plain：纯文本数据
         * ……
         */
        public static final String CONTENT_TYPE_STR = "content-type";

        /**
         * 未知的二进制数据流
         * 如文件下载、压缩文件、音频、视频等。浏览器在接收到这种类型的响应时，通常会提示用户下载该文件，而不是在浏览器窗口中直接显示数据
         */
        public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";

        /**
         * 告知浏览器文件的大小
         */
        public static final String CONTENT_LENGTH = "Content-Length";

        /**
         * attachment表示以附件方式下载 inline表示在线打开
         * "Content-Disposition: inline; filename=文件名.mp3"
         */
        public static final String CONTENT_DISPOSITION_VALUE_PREFIX_STR = "attachment;fileName=";

        /**
         * Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
         */
        public static final String CONTENT_DISPOSITION = "Content-Disposition";


    }

    /**
     * Http 的响应头对应值
     */
    public static class ResHeaderValue{
        /**
         * 下载zip
         */
        public static final String CONTENT_ZIP = "application/zip;charset=UTF-8";

        /**
         * 下载pdf
         */
        public static final String CONTENT_PDF = "application/pdf;charset=UTF-8";

        /**
         * 下载html
         */
        public static final String CONTENT_HTML = "text/html;charset=UTF-8";

        /**
         * 下载图片
         */
        public static final String CONTENT_JPEG = "image/jpeg;charset=UTF-8";
    }

    /**
     * 添加跨域的响应头
     *
     * @param response
     */
    public static void addCorsResponseHeader(HttpServletResponse response) {
        response.setHeader(CorsConfigEnum.CORS_ORIGIN.getKey(), CorsConfigEnum.CORS_ORIGIN.getValue());
        response.setHeader(CorsConfigEnum.CORS_CREDENTIALS.getKey(), CorsConfigEnum.CORS_CREDENTIALS.getValue());
        response.setHeader(CorsConfigEnum.CORS_METHODS.getKey(), CorsConfigEnum.CORS_METHODS.getValue());
        response.setHeader(CorsConfigEnum.CORS_MAX_AGE.getKey(), CorsConfigEnum.CORS_MAX_AGE.getValue());
        response.setHeader(CorsConfigEnum.CORS_HEADERS.getKey(), CorsConfigEnum.CORS_HEADERS.getValue());
        response.addHeader("Accept-Ranges","bytes");
    }

    /**
     * 添加下载响应头
     * @param response
     * @param contentTypeValue
     */
    public static void addCommonResponseHeader(HttpServletResponse response, String contentTypeValue) {
        HttpUtil.addCorsResponseHeader(response);
        response.setHeader(ResHeaders.CONTENT_TYPE_STR, contentTypeValue);
        response.setContentType(contentTypeValue);
    }


    /**
     * 跨域设置枚举类
     */
    public enum CorsConfigEnum {
        /**
         * 允许所有远程访问
         */
        CORS_ORIGIN("Access-Control-Allow-Origin", "*"),
        /**
         * 允许认证
         */
        CORS_CREDENTIALS("Access-Control-Allow-Credentials", "true"),
        /**
         * 允许远程调用的请求类型
         */
        CORS_METHODS("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT"),
        /**
         * 指定本次预检请求的有效期，单位是秒
         */
        CORS_MAX_AGE("Access-Control-Max-Age", "3600"),
        /**
         * 允许所有请求头
         */
        CORS_HEADERS("Access-Control-Allow-Headers", "*");

        CorsConfigEnum(String key, String value) {
            this.key = key;
            this.value = value;
        }

        private String key;
        private String value;

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }
}
